<?php
abstract class WebAbstract
{
    public $config; //  系统配置文件
    public $tpl;    //  视图名称
    public $request; //  请求参数
    public $db;        //   数据库驱动
    public $title;      //  页面头部
    public $script;     //  页面头部脚本加载
    public function __construct()
    {
        $this->config = config();
        $request = parseAddr();
        $this->request = $request;
        $this->dbAction();
        if(isset($_POST['ajax']) && $_POST['ajax'] == 'Y'){//   ajax 访问处理
            $data = $_POST;
            unset($data['ajax']);
            if(isset($data['uid'])) unset($data['uid']);
            $ajaxReturn = $this->ajaxAction($data);
            $this->ajaxReturn($ajaxReturn);
        }elseif(isset($_POST['class_action'])){//    post Action 请求制作后台数据处理
            $action = $_POST['class_action'];
            unset($_POST['class_action']);
            eval('$this->'.$action.'Action();');
        }else{            
            $this->pageInit();  //    页面初始化
            $tpl = $this->tpl;
            $this->headAction();    //    头部自动处理
            if($tpl == 'SELF'){// self自定义视图模板
                $this->action();    //    页面运行
            }else{                
                if(empty($tpl)) $tpl = $request['name'];
                $this->viewAction();    //    smarty 访问接口                  
                $this->action($tpl);    //    页面运行
                if($this->config->url_rewite != 'HTML' && substr_count($tpl,'.html')<1) $tpl .= '.html';
                //if(substr_count($tpl,'.html')>0) $tpl .= '.html';
                //debug($tpl);
                $this->display($tpl);
            }
            
        }        
    }
    /*------------------------------------------------- smarty 模板应用，外壳应用 -----------------------------------------------------------------------------*/
    //   smarty 模板引擎
    public function viewAction()
    {
        $smty = (object)$this->config->smarty;
        require_once($smty->dir);
        $this->view = new Smarty();
        $this->view->cache_dir = $smty->cache;
        $this->view->compile_dir =$smty->templates_c;		//http://www.cnblogs.com/gbyukg/archive/2012/06/12/2542530.html
        $this->view->debugging = $smty->debugging;
        $this->view->caching = $smty->caching;
        $this->view->cache_lifetime = $smty->cache_lifetime;
    }
	public function display($fname){//渲染tpl模板
        $dir = $this->config->view;//   从配置文件里面读出view视图目录
        $fname = $dir.$fname;
		$this->view->display($fname);
	}
	public function assign($name,$var,$nocache=null)
	{
		$nocache = $nocache? $nocache : _snocache;// “Y/N”
		$nocache = $nacache == 'Y'? true : false;
		$this->view->assign($name,$var,$nocache);
	}
    /*------------------------------------------------- 框架级处理 -----------------------------------------------------------------------------*/
    public function headAction()
    {
        if(!$this->config->headtpl) return false;// 配置文件检测
        $title = $this->title;//    标题
        $script = $this->script;//  脚本
        $tpl = $this->config->view;
        $tpl .= 'head.tpl';
        require_once($tpl);
    }
    //  页面跳转URL
    public function forward($name){forward($name,$this->config);}
    /*------------------------------------------------- 数据库驱动 -----------------------------------------------------------------------------*/
    public function dbAction($db=null)
    {
        //  数据库驱动直接赋值
        if(is_object($db)){
            $this->db = $db;
            return true;
        }
        //  获取数据库配置文件
        $dbConfig = $this->config->database;
        if(!isset($dbConfig['drive']) || $dbConfig['drive'] != 'Y') return false;// 人为关闭数据库驱动
        //  待引入数据库驱动
    }
    //  页面自动加载
    public function pageLoad($opt)
    {
        if(!is_array($opt)) return false;
        if(isset($opt['title'])) $this->title = $opt['title'];
        $xhtml = '';
        $dir = $this->config->dir;
        $public = $this->config->public;
        if(isset($opt['js'])){
            if(!is_array($opt['js'])) $xhtml .= '<script src="'.$public['js'].$opt['js'].'.js"></script>';
            else{
                foreach ($opt['js'] as $v) {$xhtml .= '<script src="'.$public['js'].$v.'.js"></script>';}
            }
        }
        if(isset($opt['css'])){
            if(!is_array($opt['css'])) $xhtml .= '<link href="'.$public['css'].$opt['css'].'.css" type="text/css" rel="stylesheet">';
            else{
                foreach ($opt['css'] as $v) {$xhtml .= '<link href="'.$public['css'].$v.'.css" type="text/css" rel="stylesheet">';}
            }
        }
        $this->script = $xhtml;
    }
    /*--------------------------------------------接口化处理*/
    public function pageInit(){}
    public function action(){}
    public function debug($data){debug($data);}
    public function ajaxAction($data){return $data;}//  ajax处理接口
    /*--------------------------------------------数据方法处理*/
    /*ajax数据返回*/
    public function ajaxReturn($data,$type='json')
    {
        if(!is_array($data)) exit($data);//下面的方法js无法获取数据
		switch (strtoupper($type)){
            case 'JSON' :
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($data,$json_option));
            case 'XML'  :
                // 返回xml格式数据
                header('Content-Type:text/xml; charset=utf-8');
                exit(xml_encode($data));
		}
    }
}